package com.aisile.utils.common;

import com.aisile.model.common.dtos.ResponseResult;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Base64;

/**
 * @Auther:yry
 * @Date:2023/12/26 0026
 * @VERSON:1.0
 */
public class ImageToBase64Utils {

    /**
     *
     * @param fileInputStream 文件流
     * @return
     */
    public static String toBase64FromImage(FileInputStream fileInputStream){
        try (
                FileInputStream fis = fileInputStream;
                ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            byte[] imageBytes = bos.toByteArray();
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
